package com.ampaiva.app;

import java.io.Serializable;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.ampaiva.db.DbProvider;

public abstract class CustomListActivity<T extends Serializable> extends ListActivity {
    Class<T> c;
    Class<?> Rlayoutclass;
    Class<?> Ridclass;
    protected DbProvider<T> db;

    public CustomListActivity(Class<T> c, Class<?> Rlayoutclass, Class<?> Ridclass) {
        this.c = c;
        this.Rlayoutclass = Rlayoutclass;
        this.Ridclass = Ridclass;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new DbProvider<T>(c, this);

        int id = ResourceUtil.getResourceByName(Rlayoutclass, "list_" + c.getSimpleName().toLowerCase());
        setListAdapter(new CustomArrayAdapter<T>(this, Ridclass, id, getList()));
    }

    protected abstract List<T> getList();

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        // get selected items
        @SuppressWarnings("unchecked")
        T selectedValue = (T) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue.toString(), Toast.LENGTH_SHORT).show();

    }
}
